package com.example.his.api.front.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.GoodsDao;
import com.example.his.api.db.dao.GoodsSnapshotDao;
import com.example.his.api.db.pojo.GoodsSnapshotEntity;
import com.example.his.api.front.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service("FrontGoodsServiceImpl")
@Slf4j
public class GoodsServiceImpl implements GoodsService {
  
  @Resource
  private GoodsDao goodsDao;
  @Autowired
  private GoodsSnapshotDao goodsSnapshotDao;
  
  
  @Override
  @Cacheable(cacheNames = "goods", key = "#id")
  public HashMap searchById(int id) {
    HashMap param = new HashMap() {{
      put("id", id);
      put("status", true);
    }};
    
    HashMap map = goodsDao.searchById(param);
    if(map != null) {
      String[] column = new String[] { "tag", "checkup_1", "checkup_2", "checkup_3", "checkup_4" };
      for(String key: column) {
        String str = MapUtil.getStr(map, key);
        JSONArray array = JSONUtil.parseArray(str);
        map.replace(key, array);
      }
      return map;
    }
    return null;
  }
  
  
  @Override
  public HashMap searchIndexGoodsByPart(Integer[] partIds) {
    HashMap map = new HashMap();
    for(int partId: partIds) {
      ArrayList <HashMap> list = goodsDao.searchByPartIdLimit4(partId);
      map.put(partId, list);
    }
    
    return map;
  }
  
  @Override
  public PageUtils searchListByPage(Map param) {
    ArrayList <HashMap> list = new ArrayList <>();
    long count = goodsDao.searchListCount(param);
    if(count > 0) {
      list = goodsDao.searchListByPage(param);
    }
    
    int page = MapUtil.getInt(param, "page");
    Integer length = MapUtil.getInt(param, "length");
    PageUtils pageUtils = new PageUtils(list, count, page, length);
    
    return pageUtils;
  }
  
  @Override
  public HashMap searchSnapshotById(String snapshotId, Integer customerId) {
    //    TODO 如果customerid不为空，检查该客户是否拥有该订单快照
    
    GoodsSnapshotEntity goodsSnapshotEntity = goodsSnapshotDao.searchById(snapshotId);
    HashMap map = BeanUtil.toBean(goodsSnapshotEntity, HashMap.class);
    return map;
    
  }
  
}
